﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Check_Printing_Solution
{
    public partial class ManageBank : Form
    {
        PrintManager manager = new PrintManager();
        Operations operations;

        public ManageBank()
        {
            InitializeComponent();
        }

        public ManageBank( Operations ops )
        {
            operations = ops;
            InitializeComponent();
        }

        private void btnSave_Click( object sender, EventArgs e )
        {
            if ( !manager.BranchExists( txtBranchPrefix.Text, txtName.Text ) )
            {
                if ( txtName.Text.Trim() != string.Empty )
                {
                    Bank bank = new Bank();
                    bank.Id = Guid.NewGuid().ToString();
                    bank.BankAddress = txtBankAddress.Text;
                    bank.BranchCode = txtBranchCode.Text;
                    bank.BranchPrefix = txtBranchPrefix.Text;
                    bank.BranchSequence = txtBranchSeq.Text;
                    bank.Name = txtName.Text.Trim();
                    bank.ChequeType = txtCheckType.Text.Trim();

                    manager.AddBank( bank );
                    ClearAddFields();
                    RefreshDataGrid();
                    MessageBox.Show( "Bank " + bank.Name + " added successfully", "Bank Added", MessageBoxButtons.OK, MessageBoxIcon.Information );
                }
                else
                {
                    MessageBox.Show( "Please add a branch name before saving.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
            }
            else
            {
                MessageBox.Show( "This branch already exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void btnCancel_Click( object sender, EventArgs e )
        {
            this.Close();
        }

        private void AddBank_Load( object sender, EventArgs e )
        {
            RefreshDataGrid();
        }

        private void RefreshDataGrid()
        {
            dgBanks.DataSource = manager.GetAllBanks();
            dgBanks.Columns[0].Visible = false;
            dgBanks.Columns["Customers"].Visible = false;
            dgBanks.Columns["Name"].Width = 200;
        }

        private void dgBanks_SelectionChanged( object sender, EventArgs e )
        {
            if ( dgBanks.SelectedRows.Count > 0 )
            {
                string bankid = dgBanks.SelectedRows[0].Cells["Id"].Value.ToString();
                PopulateBank( manager.GetBankById( bankid ) );
            }
        }

        private void PopulateBank( Bank bank )
        {
            txtBankAddressU.Text = bank.BankAddress;
            txtBranchCodeU.Text = bank.BranchCode;
            txtBranchPrefixU.Text = bank.BranchPrefix;
            txtBranchSeqU.Text = bank.BranchSequence;
            txtNameU.Text = bank.Name;
            txtCheckTypeU.Text = bank.ChequeType;
        }

        private void ClearAddFields()
        {
            txtBankAddress.Text = "";
            txtBranchCode.Text = "";
            txtBranchPrefix.Text = "";
            txtBranchSeq.Text = "";
            txtName.Text = "";
            txtCheckType.Text = "";
        }

        private void btnDelete_Click( object sender, EventArgs e )
        {
            if ( MessageBox.Show( "Are you sure you want to delete this record?", "Delete Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == System.Windows.Forms.DialogResult.Yes )
            {
                Bank bank = new Bank();
                bank.Id = dgBanks.SelectedRows[0].Cells["Id"].Value.ToString();
                manager.DeleteBank( bank );
                RefreshDataGrid();

                MessageBox.Show( "Bank " + txtNameU.Text + " deleted successfully", "Bank Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information );
            }
        }

        private void btnUpdate_Click( object sender, EventArgs e )
        {
            if ( manager.BranchExists( txtBranchPrefixU.Text, txtNameU.Text ) )
            {
                if ( txtNameU.Text.Trim() != string.Empty )
                {
                    Bank bank = new Bank();
                    bank.BankAddress = txtBankAddressU.Text;
                    bank.BranchCode = txtBranchCodeU.Text;
                    bank.BranchPrefix = txtBranchPrefixU.Text;
                    bank.BranchSequence = txtBranchSeqU.Text;
                    bank.Name = txtNameU.Text;
                    bank.ChequeType = txtCheckTypeU.Text;
                    bank.Id = dgBanks.SelectedRows[0].Cells["Id"].Value.ToString();

                    manager.UpdateBank( bank );
                    MessageBox.Show( "Bank " + txtNameU.Text + " updated successfully", "Bank Updated", MessageBoxButtons.OK, MessageBoxIcon.Information );

                    RefreshDataGrid();
                }
                else
                {
                    MessageBox.Show( "Please add a branch name before saving.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
            }
            else
            {
                MessageBox.Show( "This branch already exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }
    }
}
